package kr.co.handysoft.ft.policelogchecker;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.*;
import javax.swing.table.TableModel;

class MainDisplay extends JFrame implements ActionListener {

	/**
	 * 
	 */
	Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    
	private Properties info;
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;

	private static final long serialVersionUID = 1L;
	JFrame jfr = null;
	JLabel forProcess = new JLabel("수배차량 검색시스템");
	JButton dbCon, shortcut01, shortcut02, shortcut03, shortcut04, shortcut05, shortcut06, resultPrint, actionQuery;
	JTextField logName;
	JTextArea resultArea, queryField;
	ImageIcon exeIcon;
	Choice saveQuery;
	JScrollPane resultPane;
	JTable resultTable;
	
	private int x, y;
	private File file = null;

	public MainDisplay(){

		super("DB Management - developed by FlowerTeacher in WingFl.inc");

		launchFrame();

		setSize(1000, 600);
		x = screenSize.width / 2 - this.getWidth() / 2;  
        y = screenSize.height / 2 - this.getHeight() / 2;  
        setLocation(x, y);
		setVisible(true);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	public void launchFrame(){

		dbCon = new JButton("로그DB 연결");
		shortcut01 = new JButton("통과차량 검색");
		shortcut02 = new JButton("개인별 검색");
		shortcut03 = new JButton("차량번호 검색");
		shortcut04 = new JButton("단말기 검색");
		shortcut05 = new JButton("관서별 검색");
		shortcut06 = new JButton("일일 검색");
		resultPrint = new JButton("보고서 출력");
		
		JPanel panNorth = new JPanel();
		
		forProcess.setFont(new Font("바탕", Font.CENTER_BASELINE, 30));
		panNorth.add(forProcess);
		add(panNorth, BorderLayout.NORTH);

		JPanel panEast = new JPanel(new GridLayout(8,1));

		panEast.add(dbCon);
		dbCon.addActionListener(this);
		panEast.add(shortcut01);
		shortcut01.addActionListener(this);
		panEast.add(shortcut02);
		shortcut02.addActionListener(this);
		panEast.add(shortcut03);
		shortcut03.addActionListener(this);
		panEast.add(shortcut04);
		shortcut04.addActionListener(this);
		panEast.add(shortcut05);
		shortcut05.addActionListener(this);
		panEast.add(shortcut06);
		shortcut06.addActionListener(this);
		panEast.add(resultPrint);
		resultPrint.addActionListener(this);
		add(panEast, BorderLayout.EAST);

		JPanel panCenter = new JPanel();
		
		panCenter.setLayout(new BorderLayout());
		resultPane = new JScrollPane(resultTable = new JTable());
		panCenter.add(resultPane);
		add(panCenter, BorderLayout.CENTER);

		saveQuery = new Choice();
		saveQuery.add("default");
		saveQuery.add("shortcut01");
		saveQuery.add("shortcut02");
		saveQuery.add("shortcut03");
		saveQuery.add("shortcut04");
		saveQuery.add("shortcut05");
		saveQuery.add("shortcut06");

		JPanel panSouth = new JPanel();
		
		queryField = new JTextArea("", 5, 70);
		queryField.setEditable(true);
		exeIcon = new ImageIcon("img/police.jpg");
		actionQuery = new JButton(exeIcon);
		panSouth.add(saveQuery);
		panSouth.add(queryField);
		panSouth.add(actionQuery);
		actionQuery.addActionListener(this);
		add(panSouth, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == dbCon){
			try {
				dbConnect();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut01) {
			this.forProcess.setText(shortcut01.getText());
			String logSql = "SELECT CAST(PL_USER.USER_ID as BINARY), PL_USER.PWD, USER_INFO.AUTH " +
					"FROM PL_USER INNER JOIN USER_INFO WHERE PL_USER.USER_ID = 'woosan' AND USER_INFO.USER_ID = 'woosan'";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut02) {
			this.forProcess.setText(shortcut02.getText());
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut03) {
			this.forProcess.setText(shortcut03.getText());
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut04) {
			this.forProcess.setText(shortcut04.getText());
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut05) {
			this.forProcess.setText(shortcut05.getText());
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == shortcut06) {
			this.forProcess.setText(shortcut06.getText());
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == resultPrint) {
			this.logName.setText("수배차량 검색시스템");
			String logSql = "SELECT * FROM PL_USER";
			try {
				executeSQL(logSql);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if (e.getSource() == actionQuery) {
			this.logName.setText("수배차량 검색시스템");
//			String logSql = queryField.getText();
//			try {
//				executeSQL(logSql);
//			} catch (SQLException e1) {
//				e1.printStackTrace();
//			}
		}
	}
	
	public void dbConnect() throws Exception{
		this.file = new File("./info/dbInfo.properties");
		this.info = new Properties();
		this.info.load(new FileInputStream(file));
		Class.forName(this.info.getProperty("DBDRIVER"));
		this.conn = DriverManager.getConnection(this.info.getProperty("DBURL"),
				this.info.getProperty("DBID"), this.info.getProperty("DBPWD"));
		
//		String id = "waviss";
//		String pwd = "waviss";
//		String url = "jdbc:oracle:thin:@163.0.1.205:1521/waviss";
//		this.conn = DriverManager.getConnection(url, id, pwd);
		JOptionPane.showMessageDialog(jfr, "경찰청 DB에 연결되었습니다.");
	}
	
	public void getStatement() throws SQLException{
		this.stmt = conn.createStatement();
	}
	
	public void executeSQL(String exeSql) throws SQLException {
		this.getStatement();
		String sql = exeSql.trim();
		if(sql.lastIndexOf(";") > 0) 
			sql = sql.substring(0, sql.length() - 1);
		java.util.StringTokenizer st = 
			new java.util.StringTokenizer(sql);
		if(st.nextToken().toUpperCase().equals("SELECT")){
			ResultSet rs = stmt.executeQuery(sql);
			TableModel tm = new MyTableModel(rs);
			this.resultTable.setModel(tm);
		}else{
			int rowcount = stmt.executeUpdate(sql);
			JOptionPane.showMessageDialog(jfr, 
					rowcount + "행이 반영되었습니다.");
		}
		this.rs = this.stmt.executeQuery(sql);
	}

	public void dbClose() throws SQLException {
		this.conn.close();
	}
	
	public void stmtClose() throws SQLException {
		this.stmt.close();
	}
	
	public void rsClose() throws SQLException {
		this.rs.close();
	}
}